using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ConditionList : MonoBehaviour
{
    public IfParameter   m_IfParameter;
    public ThenParameter m_ThenParameter;
    public List<ConditionItem> m_ConditionItems = new List<ConditionItem>();
    public ConditionItem m_ConditionItemPrefab;
    public Ship m_Ship = null;
    public InputField m_SelectedItemNameEdit;
    ConditionItem m_SelectedItem;
    void Start()
    {
        m_ThenParameter.m_ConditionList = this;
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void Init(Ship ship)
    {
        m_Ship = ship;
    }
    public void InitData()
    {
        ClearList();
        for(int i=0; i< m_Ship.m_ShipEvents.Count; ++i)
        {
            AddItem(m_Ship.m_ShipEvents[i]);
        }
    }
    public void OnAddConditionClicked()
    {
        AddItem(new ShipEvent());
    }
    void AddItem(ShipEvent shipEvent)
    {
        var item = Instantiate(m_ConditionItemPrefab);
        item.Init(shipEvent);
        item.m_OnClicked = OnItemSelected;
        item.transform.SetParent(transform);
        item.transform.SetSiblingIndex(transform.childCount - 2);
        m_ConditionItems.Add(item);
        OnItemSelected(item);
    }

    void ClearList()
    {
        foreach(var i in m_ConditionItems)
        {
            Destroy(i.gameObject);
        }
        m_ConditionItems.Clear();
    }

    void OnItemSelected(ConditionItem item)
    {
        ApplySelectedItem();
        m_SelectedItem = item;
        foreach (var i in m_ConditionItems)
        {
            i.SetBackgroundState(ConditionItem.BackgroundState.Unselect);
        }
        m_SelectedItemNameEdit.text = m_SelectedItem.m_ShipEvent.m_Name;
        item.SetBackgroundState(ConditionItem.BackgroundState.Selected);
        m_IfParameter.InitIfParameter(item);
        m_ThenParameter.InitThenParameter(item);
    }
    public void ApplyAllLogic()
    {
        m_Ship.m_ShipEvents.Clear();
        foreach(var i in m_ConditionItems)
        {
            m_Ship.m_ShipEvents.Add(i.m_ShipEvent);
        }
    }

    public void ApplySelectedItem()
    {
        if (m_SelectedItem == null)
            return;
        List<List<ShipEventTrigger>> shipTriggers = new List<List<ShipEventTrigger>>();
        foreach (var i in m_IfParameter.m_IfItems)
        {
            var orTriggers = i.GetShipEventTriggers();
            shipTriggers.Add(orTriggers);
        }
        List<ShipEventBehaviours> behaviours = new List<ShipEventBehaviours>();
        foreach (var i in m_ThenParameter.m_ThenItems)
        {
            var behavour = i.GetShipBehaviour();
            if (behavour != null)
                behaviours.Add(behavour);
        }

        m_SelectedItem.m_ShipEvent.m_Events = new KeyValuePair<List<List<ShipEventTrigger>>, List<ShipEventBehaviours>>(shipTriggers, behaviours);
    }

    public void OnSelectedItemNameChanged(string name)
    {
        m_SelectedItem.SetName(name);
    }
}
